 function getAjaxObject(){
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser doesn't suppport Ajax!");
				return false;
			}
		}
	}
	return ajaxRequest;
}
/*
function getcommodity(){
	alert('hi');
	var rows = document.getElementById('tt').getElementsByTagName('tbody')[0].rows.length;
	var tmpNum = rows+1 ; 
	alert(tmpNum);
	var res1=getAjaxObject();
	res1.onreadystatechange=function(){
		if(res1.readyState==4){
			var tt=res1.responseText;
			if(tt!=''){
				document.getElementById(showContentDivID).innerHTML=tt;
			}
		}
	}
	res1.open("GET","" ,true);
	res1.send(null);	
}*/

/*function getcommodityList(){	
	var dataArr = [
		{"productid":"5-Apr-14","unitcost":29875,"status":5,"listprice":1,"attr1":29878,"id":"GOLD","list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"cost":-0.35},
		{"productid":"31-Mar-14","unitcost":120.7,"status":13,"listprice":7,"attr1":120.75,"id":"ZINC","list":-0.4,"unit":121,"attr":121.1,"low":120.6,"price":121.1,"cost":-0.33},
		{"productid":"30-Apr-14","unitcost":45453,"status":1,"listprice":2,"attr1":45460,"id":"SILVER","list":-223,"unit":45729,"attr":45729,"low":45438,"price":45673,"cost":-0.49},
	{"productid":"31-Mar-14","unitcost":103.6,"status":8,"listprice":4,"attr1":103.65,"id":"ALUMINIUM","list":-0.4,"unit":103.6,"attr":103.85,"low":103.5,"price":104,"cost":-0.38},
	{"productid":"30-Apr-14","unitcost":403.05,"status":4,"listprice":8,"attr1":403.15,"id":"COPPER","list":-3.35,"unit":405.9,"attr":405.9,"low":402.55,"price":406.5,"cost":-0.82},
	{"productid":"31-Mar-14","unitcost":126.3,"status":19,"listprice":16,"attr1":126.35,"id":"LEAD","list":-1.05,"unit":126.7,"attr":127.1,"low":126.3,"price":127.4,"cost":-0.82},
	{"productid":"21-Apr-14","unitcost":6117,"status":13,"listprice":7,"attr1":6118,"id":"CRUDEOIL","list":55,"unit":6080,"attr":6131,"low":6080,"price":6063,"cost":0.91},
	{"productid":"31-Mar-14","unitcost":863,"status":4,"listprice":1,"attr1":863.4,"id":"MENTHAOIL","list":-1.8,"unit":867,"attr":869,"low":857.2,"price":864.8,"cost":-0.21},
	{"productid":"31-Mar-14","unitcost":987.4,"status":1,"listprice":1,"attr1":987.7,"id":"NICKEL","list":-6.5,"unit":991.3,"attr":993.3,"low":986.7,"price":994.1,"cost":-0.65}
	
	];
	//alert(dataArr);
	for(var obj in dataArr){
		//alert('new array >>'+dataArr[obj]);
		var newRow = document.all("tt").insertRow(); 
		for(var property in dataArr[obj]){
			//alert(property);
			//alert(dataArr[obj][property]);
			 var oCell = newRow.insertCell();
			oCell.innerHTML =  "<span style=' float:left; text-align:centre; font-size:14px;'>"+dataArr[obj][property]+" </span>";
		}
	}	
}
*/

function callFunction(){
		//alert('yes');
		var id = document.getElementById('prdid').value;
		
		var productid = document.getElementById('timeid').value;
		var select = document.getElementById("timeid");
		$('#timeid option[value!="0"]').remove();
		
		var option = document.createElement("option");
		 
		option.value ="";
        option.textContent ="-Select-";
        select.appendChild(option);
        var option1 = document.createElement("option");
	     
	     option1.value ="31Jul2014";
	        option1.textContent ="31Jul2014";
	        select.appendChild(option1);
	        
	        var option2 = document.createElement("option");
		        
	        option2.value ="30Aug2014";
	        option2.textContent ="30Aug2014";
	        select.appendChild(option2);
	        //alert(id);
		//alert(productid);
		getcommodity(id, productid);
}

function getcommodity(id, productid){	
	var dataArr = [
{"id":"ALUMINIUM L","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"ALUMINIUM S","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"BRCRUDE OIL","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"COPPER L","productid":"31Jul2014","status":5,"unitcost":150,"attr1":105,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"COPPER S","productid":"31Jul2014","status":5,"unitcost":150,"attr1":158,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"CRUDE OIL","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"GOLD L","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"GOLD S","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"LEAD L","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"LEAD S","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"NATURAL GAS","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"SILVER L","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"SILVER S","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"ZINC L","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"ZINC S","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"NICKEL L","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"NICKEL S","productid":"31Jul2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"ALUMINIUM L","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"ALUMINIUM S","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"BRCRUDE OIL","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"COPPER L","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"COPPER S","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"CRUDE OIL","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"GOLD L","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"GOLD S","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"LEAD L","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"LEAD S","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"NATURAL GAS","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"SILVER L","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"SILVER S","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"ZINC L","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"ZINC S","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"NICKEL L","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541},
{"id":"NICKEL S","productid":"30Aug2014","status":5,"unitcost":29875,"attr1":29878,"listprice":1,"list":-105,"unit":29930,"attr":29973,"low":29864,"price":29981,"ltp":29875,"cost":-0.35,"atp":27819,"dpr":32152,"voll":145245,"trend":788,"lth":32255,"ltl":26541}
];
	
	
	
		//alert("Inside another function");
      //  $("#tt").find("tr:gt(0)").remove();

        var table1 = $("#tblDataGrid");
     
        
        $.each(dataArr, function(key,value) {
        	var rows=document.all("tblDataGrid").rows.length;
        	var rowid = 'row'+rows;
        //	alert("Rows: "+rowid)
       // 	 alert(value['id']+' ' + id);
     	//	alert(value['productid'] + ' ' + productid );
        	if(value['id'] == id && value['productid'] == productid){
        var rowNew = $("<tr id="+rowid+" onclick=function (evt){  selectRow(rowid);};>"+
        		"<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
                rowNew.children().eq(0).text(value['id']);
                rowNew.children().eq(1).text(value['productid']);
                rowNew.children().eq(2).text(value['status']);
                rowNew.children().eq(3).text(value['unitcost']);
                rowNew.children().eq(4).text(value['attr1']);
                rowNew.children().eq(5).text(value['listprice']);
                rowNew.children().eq(6).text(value['list']);
                rowNew.children().eq(7).text(value['unit']);
                rowNew.children().eq(8).text(value['attr']);
                rowNew.children().eq(9).text(value['low']);
                rowNew.children().eq(10).text(value['price']);
                rowNew.children().eq(11).text(value['ltp']);
                rowNew.children().eq(12).text(value['cost']);
                rowNew.children().eq(13).text(value['atp']);
                rowNew.children().eq(14).text(value['dpr']);
                rowNew.children().eq(15).text(value['voll']);
                rowNew.children().eq(16).text(value['trend']);
                rowNew.children().eq(17).text(value['lth']);
                rowNew.children().eq(18).text(value['ltl']);             
                rowNew.appendTo(table1);
        	 } 
        });
        
	/*//alert(dataArr);
	for(var obj in dataArr){
	//	alert('new array >>'+dataArr[obj]);
		var newRow = document.all("tt").insertRow(); 
		var rows = document.all("tt").rows.length;
		//alert("Length: "+rows);
		rows++;
		var rowid = 'row'+rows;
		alert(rowid);
		newRow.id =rowid;
		newRow.onclick = function (evt){  selectRow(rowid);};
		//alert('new id >> '+id);
		var productArr = [];
		for(var property in dataArr[obj]){
			//alert(property);
			//alert(dataArr[obj]['id']);
			
		}
		//alert(dataArr[obj]['id']+' ' + id);
		//alert(dataArr[obj]['productid'] + ' ' + productid );
		if(dataArr[obj]['id'] == id && dataArr[obj]['productid'] == productid){
		//	alert('aa gya');
			productArr = dataArr[obj];
			
		}
		//alert(productArr);
		for(var property in productArr){
		//	alert(property);
		//	alert(productArr[property]);
			 var oCell = newRow.insertCell();
			oCell.innerHTML =   productArr[property];			
		}
	}*/
}

function selectRow(rowid){
	var rows = document.all("tt").rows.length;
	//alert(rows);
	var count = rows+1;
	for(var index =2; index<=count; index++){
		var tmp = "row"+index;
		//alert(tmp);
		document.getElementById(tmp).style.border = "none";
	}
	document.getElementById(rowid).style.border = "1px solid #000000";
	
   
}

function getBuyJsonAndOpenNewWindow(){
//	var userId= ('<sec:authentication property="principal.username"/>').replace('&#32;',' ');
	var buyJson=JSON.stringify({ "otype":"buy","insname":"1111","symbol":"GOLD"
		,"expdate":"30-Apr-2014","qt":"5565","ql":"55","qw":"55","price":"4",
		"ptype":"8","tp":"5","userId":"user"});

	$.ajax( 
			  {
			  type: "POST",
			  url: "./order/buy",			 
			  contentType: 'application/json',			  
			  data:buyJson ,
			  success: function(result) { 			 
			    //alert(result);
			  }
			  
			});
	
}

/*$(document).ready(function() { 
	
	$("#tt").click(function(event) { 
	//var reqUrl=($("#orderType").val()=="BUY")?"./buy":"./sell";
	var reqUrl="./order/buy";
	var userId= ('<sec:authentication property="principal.username"/>').replace('&#32;',' ');
	var buyJson=JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),"price":$("#price").val(),"ptype":$("#ptype").val(),"tp":$("#tp").val(),"userId":userId});
	//alert(buyJson);
	$.ajax(
		  {
		  type: "POST",
		  url: reqUrl,
		  dataType: "json",
		  contentType: 'application/json',
		  
		  data: JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),"price":$("#price").val(),"ptype":$("#ptype").val(),"tp":$("#tp").val(),"userId":userId}),
		  success: function(html) { 			 
		    alert(html);
		  }
		  
		});
	});
});*/

